package com.lo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lo.common.JsonResult;
import com.lo.dao.CourseMapper;
import com.lo.entity.Course;
import com.lo.entity.CourseWithTCHName;
import com.lo.entity.Message;
import com.lo.server.WebSocketServer;
import com.lo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import  com.lo.service.impl.UserBasedCollaborativeFiltering;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public JsonResult addCourse(Course course) {
        int rows = courseMapper.insert(course);
        if (rows <=0){
            return new JsonResult(5,"课程上传失败",null);
        }
        return new JsonResult(0,"课程上传成功",course);
    }



    @Override
    public JsonResult deleteCourse(Long id) {
        List<Course> courses = courseMapper.selectCourseById(id);
        String tid = courses.get(0).getTid().toString();
        int rows = courseMapper.deleteById(id);
        if(rows <= 0){
            throw new  RuntimeException("删除失败，请联系管理员");
        }
        Message message = new Message(0,"message","课程已删除，请查看");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult(0,"删除成功",courses.get(0));

    }

    //编辑框更新数据
    @Override
    public JsonResult updateCourse(Course course) {
        int rows = courseMapper.update(course);
        if (rows <=0){
            return new JsonResult(1,"更新失败，联系管理员",null);
        }
        return new JsonResult(0,"更新成功",null);
    }

    //编辑框回显数据
    @Override
    public JsonResult editCourse(Long id) {
        List<Course> courses = courseMapper.selectCourseById(id);
        if (courses.size()<=0){
            return new JsonResult(1,"未查到相应课程",null);
        }
        return new JsonResult(0,"查到相应课程",courses.get(0));
    }

    @Override
    public JsonResult updateIsExamine(Long id) {
        int rows = courseMapper.updateIsExamine(id);
        List<Course> courses = courseMapper.selectCourseById(id);
        String tid = courses.get(0).getTid().toString();
        if (rows <=0){
            return new JsonResult(1,"更新失败，联系管理员",null);
        }
        Message message = new Message(0,"message","课程审核通过");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult(0,"审核成功",null);
    }

    @Override
    public JsonResult updateIsDelete(Long id) {
        int rows = courseMapper.updateIsDelete(id);
        if (rows<=0){
            return new JsonResult(1,"更新失败，联系管理员",null);
        }
        return new JsonResult(0,"提交删除成功，等待管理审核",null);
    }

    @Override
    public JsonResult updateIsDelete2Zero(Long id) {
        int rows = courseMapper.updateIsDelete2Zero(id);
        List<Course> courses = courseMapper.selectCourseById(id);
        String tid = courses.get(0).getTid().toString();
        if (rows<=0){
            return new JsonResult(1,"更新失败，联系管理员",null);
        }
        Message message = new Message(0,"message","课程为通过删除，请查看");
        WebSocketServer.sendMessageByUserId(tid,message);
        return new JsonResult(0,"撤销删除成功，等待管理审核",null);
    }

    @Override
    public JsonResult courseOrdinaryCount() {
        return new JsonResult(0,"普通课程总数",courseMapper.courseOrdinaryCount());
    }

    @Override
    public JsonResult courseVipCount() {
        return new JsonResult(0,"会员课程总数",courseMapper.courseVipCount());
    }

    @Override
    public JsonResult courseOrdinaryCountByTid(Long tid) {
        return new JsonResult(0,"普通课程总数",courseMapper.courseOrdinaryCountByTid(tid));
    }

    @Override
    public JsonResult courseVipCountByTid(Long tid) {
        return new JsonResult(0,"会员课程总数",courseMapper.courseVipCountByTid(tid));
    }

    @Override
    public JsonResult selectOrdinaryCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectOrdinaryCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"普通课程列表",ordinaryCourses);

    }

    @Override
    public PageInfo selectOrdinaryCourseWithPage(int pageNum, int pageSize,String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectOrdinaryCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectVipCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectVipCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"精品课程列表",ordinaryCourses);
    }

    @Override
    public PageInfo selectVipCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectVipCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectEngineeringCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectEngineeringCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"工学课程列表",ordinaryCourses);
    }

    @Override
    public PageInfo selectEngineeringCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectEngineeringCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectScienceCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectScienceCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"理学课程列表",ordinaryCourses);
    }
    @Override
    public PageInfo selectScienceCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectScienceCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectLiteratureCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectLiteratureCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"文学课程列表",ordinaryCourses);
    }

    @Override
    public PageInfo selectLiteratureCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectLiteratureCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectLawCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectLawCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"法学课程列表",ordinaryCourses);
    }

    @Override
    public PageInfo selectLawCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectLawCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectAllCourse() {
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectAllCourse();
        if (ordinaryCourses.size()<=0){
            return new JsonResult(1,"课程为空，请联系管理上传课程",null);
        }
        return new JsonResult(0,"课程列表",ordinaryCourses);
    }

    @Override
    public PageInfo selectAllCourseWithPage(int pageNum, int pageSize, String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseWithTCHName> ordinaryCourses = courseMapper.selectAllCourseWithPage(search);
        PageInfo pageInfo = new PageInfo(ordinaryCourses);
        return pageInfo;
    }

    @Override
    public JsonResult selectAllCourseToWX(String search) {

        return new JsonResult<>(0,"视频总览",courseMapper.selectAllCourseWithPage(search));
    }



}
